/*
 * Copyright (c) 2022  by Junzhao
 * THIS FILE IS PART OF JAVASE Song Huongkang PROJECT
 * All Rights Reserved.
 */

package 线程安全.同步方法;

public class WindowsTest1 {
    public static void main(String[] args) {
        Windows1 wu = new Windows1();
        Thread t1 = new Thread(wu,"窗口1");
        Thread t2 = new Thread(wu,"窗口2");
        Thread t3 = new Thread(wu,"窗口3");

        t1.start();
        t2.start();
        t3.start();
    }
}
class Windows1 implements Runnable{
    private int ticket = 1000;
//    Object obj = new Object();

    @Override
    public void run() {
        while(true){
            show();
        }
    }
    private synchronized void show(){// 同步方法 形式 同步监视器是 this
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (ticket>0){
            System.out.println(Thread.currentThread().getName()+"售票 x1 号码为："+ticket);
            ticket--;
        }
    }
}